package com.logic.modular.gameCore.cultivate.signIn.service.impl;

import lombok.extern.slf4j.Slf4j;
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.logic.modular.gameCore.cultivate.signIn.mapper.CoreSignInAwardMapper;
import com.logic.modular.gameCore.cultivate.signIn.domain.CoreSignInAward;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import javax.annotation.PostConstruct;


/**
 * 签到奖励(CoreSignInAward)表服务实现类
 *
 * @author cxy
 * @since 2025-01-04 14:53:28
 */
@Slf4j
@Service
public class CoreSignInAwardServiceImpl extends ServiceImpl<CoreSignInAwardMapper, CoreSignInAward>  {
    private static final  Cache<String, Map<Long,CoreSignInAward>> cache = Caffeine.newBuilder().build();
    
    @PostConstruct
    private void init() {
        CoreSignInAward initSelect = CoreSignInAward.builder().build();
        List<CoreSignInAward> list = this.list(new LambdaQueryWrapper<>(initSelect));
        cache.put(
                "coreSignInAward", 
                list.stream().collect(Collectors.toMap(CoreSignInAward::getId, CoreSignInAward::init))
        );
        log.info("签到奖励数据缓存完成,数据量为:{}",list.size());
    }
    
    public static List<CoreSignInAward> listCache() {
        return new ArrayList<>(mapCache().values());
    }     
    
    public static Map<Long,CoreSignInAward> mapCache() {
        Map<Long,CoreSignInAward> values = cache.getIfPresent("coreSignInAward");
        if (Objects.isNull(values)) {
            throw new RuntimeException("数据为空");
        }
        return values;
    }   
    
    public static CoreSignInAward getCache(Long id) {
        return mapCache().get(id);
    }
}
